package com.mingqijia.gassafety.webserver.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.mingqijia.gassafety.db.entity.auth.DivisionEntry;
import com.mingqijia.gassafety.db.mapper.auth.DivisionMapper;
import com.mingqijia.gassafety.webserver.response.baidu.BaiDuDetailResp;
import com.mingqijia.gassafety.webserver.service.DivisionService;
import com.mingqijia.gassafety.webserver.utils.CoordinatesTransitionUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *
 * @author wanghai
 * @since 2022-03-04
 */

@Service
@Slf4j
public class DivisionServiceImpl extends ServiceImpl<DivisionMapper,DivisionEntry> implements DivisionService {
    @Autowired
    DivisionMapper divisionMapper;

    @Value("${baidu.maps.key}")
    private String baiDuMapsKey;

    @Override
    public Map<String, String> divisionQuery(String pathCode) {
        //根据行政区代码 adCode 将市级名进行拼接
        QueryWrapper<DivisionEntry> divisionWrapper = new QueryWrapper<>();
        divisionWrapper.lambda().eq( DivisionEntry::getPathCode, pathCode );
        DivisionEntry divisionEntry = divisionMapper.selectOne( divisionWrapper );

        Long parentId = divisionEntry.getParentId();
        DivisionEntry divisionEntryParent = this.getById( parentId );
        HashMap<String, String> map = new HashMap<>();
        map.put( "name",divisionEntry.getName() );
        map.put( "parentName",divisionEntryParent.getName() );
        return map;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void syncDivision() {
        int pageIndex = 1;
        int  pageSize = 500;
        long total = 0;
        do{
            Page<DivisionEntry> pages = PageHelper.startPage(pageIndex, pageSize);
            QueryWrapper<DivisionEntry> areaQueryWrapper = new QueryWrapper<>();
            areaQueryWrapper.lambda().gt(DivisionEntry::getLongitude, 0)
                    .gt(DivisionEntry::getLatitude, 0).eq(DivisionEntry::getIsDeleted, false);
            List<DivisionEntry> divisionEntries = this.list(areaQueryWrapper);
            pageIndex++;
            total = pages.getTotal();
            log.info("行政区划表高德转百度{},分页{},{}，{}",pageIndex, pages.getPages(), pages.getPageSize(), pages.getTotal());
            if (org.apache.commons.collections.CollectionUtils.isNotEmpty(divisionEntries)) {
                //开始修改坐标
                for (DivisionEntry divisionEntry : divisionEntries) {
                    List<BaiDuDetailResp> baiDuDetailResps = CoordinatesTransitionUtil.requestGetAK(divisionEntry.getLongitude() + "," + divisionEntry.getLatitude(), baiDuMapsKey);
                    if (CollectionUtils.isNotEmpty(baiDuDetailResps)) {
                        divisionEntry.setLongitude(baiDuDetailResps.get(0).getX().toString());
                        divisionEntry.setLatitude(baiDuDetailResps.get(0).getY().toString());
                    }
                }
                this.saveOrUpdateBatch(divisionEntries);
            }
            divisionEntries.clear();
        }while(total > (pageIndex - 1) * pageSize);
    }
}
